package com.chinasoft.service.Impl;

import com.chinasoft.mapper.TeacherMapper;
import com.chinasoft.mapper.UserMapper;
import com.chinasoft.pojo.TeacherList;
import com.chinasoft.pojo.User;
import com.chinasoft.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    UserMapper userMapper;
    TeacherMapper teacherMapper;
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User login(User acc) {
        String username = acc.getUsername();
        User account = userMapper.selectByName(username);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        String dateTime = now.format(formatter);
        //是否存在用户
        if (account == null ) {
            return null;
        }
        //比较密码
        if(acc.getPassword().equals(account.getPassword())){
            if(acc.getEndtime() != null && acc.getEndtime().compareTo(dateTime) < 0 ){
                String endtime = "";
                String type = "";
                userMapper.updateDel(username, endtime, type);
            }
            //密码一致
            return account;
        }else{
            return null;
        }
    }

    @Override
    public User getById(String teachername) {
        return userMapper.selectByName(teachername);
    }

    @Override
    public List<User> getByType(String usertype) {
        return userMapper.selectByType(usertype);
    }

    @Override
    public boolean update(String username, String type, String endtime) {
        int i = userMapper.updateDel(username, type, endtime);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean delete(String username) {
        int i = userMapper.delete(username);
        String del = "1";
        List<TeacherList> list = teacherMapper.getByTeacher(username);
        for(TeacherList list1 : list){
            teacherMapper.updateDel(list1.getId(), del);
        }
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean addTeacher(User acc) {
        int i = userMapper.insert(acc);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean addUser(User user) {
        int i = userMapper.insertUser(user);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean updateTeacher(String username, String intime) {
        int i = userMapper.update(username, intime);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }

    @Override
    public boolean setPassword(String username, String password) {
        int i = userMapper.setPassword(username, password);
        if(i == 1){
            return true;
        } else{
            return false;
        }
    }
}